package com.aos.erp.fs.service.impl;

import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.fs.enums.FileStatus;
import com.aos.erp.fs.mapper.SysFileMapper;
import com.aos.erp.fs.model.SysFile;
import com.aos.erp.fs.service.SysFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Calendar;

@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {


    @Override
    public SysFile getById(long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void addBean(SysFile bean) throws BizException {
        if (bean == null) {
            throw new BizException("文件能为空");
        }
        bean.setCreateTime(Calendar.getInstance().getTime());
        bean.setStatus(FileStatus.normal);
        baseMapper.insert(bean);
    }

    @Override
    public PageBean<SysFile> queryByPage(long pageIndex, long pageSize, String appId, String userId) {

        IPageBean<SysFile> pageBean = baseMapper.createBizPage(pageIndex, pageSize);
        Condition condition = new Condition(SysFile.class);

        if (StringUtils.isNotBlank(userId)) {
            condition.eqFiled("userId", userId);
        }
        if (StringUtils.isNotBlank(appId)) {
            condition.eqFiled("appId", appId);
        }
        condition.orderByDesc("id");

        return (PageBean<SysFile>) baseMapper.selectPage(pageBean, condition.getWrapper());
    }
}
